//
//  MgpyhCell.m
//  danpin
//
//  Created by chuangjia on 12/11/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "MgpyhCell.h"
#import "CalcLabel.h"
#import "appdelegate.h"
#import "TTTAttributedLabel.h"
#import "UILabel+StringFrame.h"
@interface MgpyhCell ()
{
    
}
@property(nonatomic,strong) TTTAttributedLabel *title;
@property(nonatomic,strong)UILabel *price;
@property(nonatomic,strong)UIImageView *lineView;
@end
@implementation MgpyhCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        
        _title =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _title.backgroundColor = [UIColor clearColor];
        _title.textColor = [UIColor darkTextColor];//UIColorFromRGB(0x808080);
        _title.textAlignment = NSTextAlignmentLeft;
        _title.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        _title.numberOfLines=1;
        _title.font = FONT(13);
        [self.contentView addSubview:_title];
        
        _price =[[UILabel alloc] initWithFrame:CGRectZero];
        _price.backgroundColor = [UIColor clearColor];
        _price.textColor = RED_COLOR;;
        _price.textAlignment = NSTextAlignmentRight;
        _price.font =  FONT(14);
        _price.numberOfLines=0;
        [self.contentView addSubview:_price];
        
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.backgroundColor=UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_lineView];
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.title=nil;
    self.price=nil;
    [self.lineView removeFromSuperview];
    self.lineView=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    self.lineView.frame =CGRectMake(0, self.bounds.size.height-0.5, DEVICE_WIDTH, 0.5);
    
}

/// 根据数据模型来显示内容
- (void)showInfo:(id)model other:(id)other key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.other=other;
    self.key = key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    
    __weak typeof (self) wSelf = self;
    NSString *title =[dict stringForKey:@"title" withDefaultValue:@""];
    if(title.length>0)
    {
        
        [self.title setText:title afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
         {
             
             [attributedString addAttribute:NSFontAttributeName
                                      value:FONT(13)
                                      range:NSMakeRange(0,[attributedString length])];

             //设置字体颜色
             [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:TITLE_COLOR range:NSMakeRange(0,[attributedString length])];
             
             
             
             NSArray* words = [wSelf.data arrayObjectForKey:@"words"];
             if(words && [words isKindOfClass:[NSArray class]])
             {
                 for(int i = 0; i < [words count]; i++)
                 {
                     
                     
                     NSString* strWord = [words objectAtIndex:i];
                     NSRange range = [[attributedString string] rangeOfString:strWord options:NSCaseInsensitiveSearch];
                     
                     if (range.location != NSNotFound)
                     {
                         [attributedString addAttribute:(id)kCTForegroundColorAttributeName
                                                  value:HILIGHT_COLOR//UIColorFromRGB(0xCE0202)//[UIColor redColor]
                                                  range:range];
                         
                     }
                 }
             }
             
     //        CFRelease(helveticaBold);
             return attributedString;
             
         }];
        
        float fWidth = DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2;
      
        
        NSString *comment =[dict stringForKey:@"price_str" withDefaultValue:@""];
        if (comment.length>0) {
            self.price.text =comment;
            CGSize sizeName = [comment boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.price.font}context:nil].size;
            
//            CGSize sizeName = [comment sizeWithFont:self.price.font constrainedToSize:CGSizeMake(MAXFLOAT, 0.0) lineBreakMode:NSLineBreakByWordWrapping];
            self.price.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH, DEVICE_WIDTH - PHOTO_FRAME_WIDTH*2, 20);
            
            fWidth = fWidth - sizeName.width - PHOTO_TIME_PADDING;
        }else
        {
            self.price.text =@"";
        }
        
        self.title.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH, fWidth, 20);
    }
    [self layoutSubviews];
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    return 40;
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)commentAction
{
    if (self.actionBlock)
    {
        self.actionBlock(BEventType_taobao,nil,self.data,self.key,self.indexPath);
    }
}
@end